﻿namespace PLCComHelperProj
{
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    using System.Xml;

    public class ClientComHelper
    {
        private readonly int Fuck_int = 0x7d0;
        private PLCLink plcSet = new PLCLink();
        private bool ProtectFlg;
        private XmlDocument xmlDocument = new XmlDocument();
        private string XmlString = Path.Combine(Application.StartupPath, FuckProtect.DataFrom(0x242));

        public Device GetDevice()
        {
            return this.plcSet.GetDevice();
        }

        public object GetValue(string name)
        {
            return this.plcSet.getValue(name);
        }

        public bool Init()
        {
            int num = 0;
            if (!File.Exists(this.XmlString))
            {
                return false;
            }
            this.plcSet.GetDevice().Clear();
            this.xmlDocument.Load(this.XmlString);
            XmlNode documentElement = this.xmlDocument.DocumentElement;
            XmlNode node2 = documentElement.SelectSingleNode("/Device");
            this.plcSet.SetIP(node2.Attributes["ip"].Value);
            this.plcSet.GetDevice().CPU_SlotNO = node2.Attributes["cpuSlotNO"].Value;
            this.plcSet.GetDevice().PLCType = node2.Attributes["PLCType"].Value;
            this.plcSet.GetDevice().LocalTASP = node2.Attributes["localTASP"].Value;
            this.plcSet.GetDevice().RemoteTASP = node2.Attributes["remoteTASP"].Value;
            XmlNodeList list = documentElement.SelectNodes("//TagGroup");
            for (int i = 0; i < list.Count; i++)
            {
                string key = list[i].Attributes["name"].Value.ToUpper();
                string mMType = list[i].Attributes["mmtype"].Value;
                int block = (list[i].Attributes["block"].Value == "") ? 0 : Convert.ToInt32(list[i].Attributes["block"].Value);
                int address = Convert.ToInt32(list[i].Attributes["beginAddress"].Value);
                int readCount = Convert.ToInt32(list[i].Attributes["readCount"].Value);
                this.plcSet.GetDevice().TagGroups.Add(key, new TagGroup(key, mMType, block, address, readCount));
                XmlNodeList list2 = list[i].SelectNodes("Tag");
                for (int j = 0; j < list2.Count; j++)
                {
                    string str3 = list2[j].Attributes["name"].Value.ToUpper();
                    string dataType = list2[j].Attributes["dataType"].Value;
                    string str5 = list2[j].Attributes["address"].Value;
                    string desc = list2[j].Attributes["desc"].Value;
                    TagGroup tagGroup = this.plcSet.GetDevice().TagGroups[key];
                    tagGroup.Tags.Add(str3, new Tag(tagGroup, str3, str5, dataType, desc));
                    num++;
                }
            }
            if (num > this.Fuck_int)
            {
                MessageBox.Show(FuckProtect.DataFrom(0x1c4));
                this.ProtectFlg = false;
                return false;
            }
            this.ProtectFlg = true;
            return this.plcSet.method_9();
        }

        public bool Start()
        {
            return (this.ProtectFlg && this.plcSet.PLC_StartConnect());
        }

        public void Stop()
        {
            this.plcSet.PLC_StopConnect();
        }

        private bool ValuTs(string str)
        {
            if (str == "")
            {
                return false;
            }
            Regex regex = new Regex(FuckProtect.DataFrom(0x20a));
            return regex.IsMatch(str);
        }

        public void WriteData(string name, double value)
        {
            if (this.ValuTs(value.ToString()))
            {
                this.plcSet.WriteData(name, value);
            }
        }

        public int CommStatus
        {
            get
            {
                return this.plcSet.commStatus();
            }
        }

        public string IP
        {
            set
            {
                this.plcSet.SetIP(value);
            }
        }

        public string TagConfigFile
        {
            get
            {
                return this.XmlString;
            }
            set
            {
                this.XmlString = value;
            }
        }
    }
}

